﻿using Microsoft.AspNetCore.Http;

namespace K9Nano.Updater.Server.Authorization;

internal class ClientContextAccessor(IHttpContextAccessor httpContextAccessor) : IClientContextAccessor
{
    private ClientContext? _client;

    public ClientContext? Client
    {
        get
        {
            if (this._client == null)
            {
                var ctx = httpContextAccessor.HttpContext;
                if (ctx != null)
                {
                    var clientId = ctx.Request.Headers[Constants.HeaderClientId].FirstOrDefault();
                    if (!string.IsNullOrEmpty(clientId))
                    {
                        this._client = new ClientContext
                        {
                            ClientId = clientId,
                        };
                    }
                }
            }
            return this._client;
        }
    }
}